home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™94 / Talks & Papers / Michael D. Crawford↵ / Word Services SDK 1.0.5 / Writeswell Jr. Source / PageSetup.c < prev    next >
Text File  |  1992-10-24  |  3KB  |  154 lines

  1. /* PageSetup.c
  2.  * Do the page setup dialog in Writeswell Jr.
  3.  *
  4.  * ©1992 Working Software, Inc.
  5.  * This source code is copyrighted.  Permission is granted to use the Word Services
  6.  * portion of the Writeswell Jr. source code in your own programs, but you 
  7.  * may not distribute the Writeswell Jr. word-processor code as a 
  8.  * commercial product.  If you modify the code, please do not call it 
  9.  * Writeswell Jr. (or Writeswell.)  This will ensure that people understand the 
  10.  * program and don’t have to deal with a number of different versions with 
  11.  * who-knows-what going on in the code.
  12.  * 
  13.  * Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
  14.  *
  15.  * 24 Oct 92 Mike Crawford 
  16.  */
  17.  
  18. #include <AppleEvents.h>
  19. #include <Printing.h>
  20. #include "TestBed.h"
  21. #include "TBConstants.h"
  22. #include "Gripe.h"
  23. #include "Prefs.h"
  24. #include "TBGlobals.h"
  25. #include "PageSetup.h"
  26.  
  27. void DoPageSetup( void )
  28. {
  29.     THPrint        thePrRecHdl;
  30.     OSErr        printErr;
  31.     Boolean        saveSetup;
  32.  
  33.     thePrRecHdl = GetPrintRecord();
  34.  
  35.     if ( !thePrRecHdl ){
  36.         Gripe( "\pCannot get print record" );
  37.         return;
  38.     }
  39.  
  40.     saveSetup = false;
  41.  
  42.     PrOpen();
  43.     
  44.     printErr = PrError();
  45.  
  46.     if ( printErr == noErr ){
  47.             
  48.         PrValidate( thePrRecHdl );
  49.                 
  50.         printErr = PrError();
  51.  
  52.         if ( printErr == noErr ){
  53.  
  54.             saveSetup = PrStlDialog( thePrRecHdl );
  55.             
  56.             printErr = PrError();
  57.             
  58.         }
  59.     }else{
  60.         Gripe( "\pCould not open the printer driver" );
  61.     }
  62.     PrClose();
  63.     
  64.     if ( PrError() || printErr || !saveSetup ){
  65.         ReleaseResource( thePrRecHdl );
  66.         thePrRecHdl = NULL;
  67.         return;
  68.     }
  69.  
  70.     ChangedResource( thePrRecHdl );
  71.     WriteResource( thePrRecHdl );
  72.     UpdateResFile( gPrefFileRefNum );
  73.  
  74.     return;
  75. }
  76.  
  77. THPrint GetPrintRecord( void )
  78. {
  79.     THPrint    thePrRecHdl;
  80.     short    curFile;
  81.     long    prRecSize;
  82.     OSErr    err;
  83.  
  84.     /* DO NOT Call this function while the printer driver is open */
  85.  
  86.     curFile = CurResFile();
  87.     UseResFile( gPrefFileRefNum );
  88.     
  89.     thePrRecHdl = (THPrint)Get1Resource( 'PStl', rPrintRecID );
  90.     
  91.     UseResFile( curFile );
  92.  
  93.     if ( thePrRecHdl ){
  94.         
  95.         /* Validate the the handle is a good one */
  96.         
  97.         prRecSize = GetHandleSize( thePrRecHdl );
  98.         if ( prRecSize != sizeof( TPrint ) ){
  99.             RmveResource( thePrRecHdl );
  100.             return (THPrint)NULL;
  101.         }
  102.         
  103.         return thePrRecHdl;
  104.     }
  105.     
  106.     /* At this point, no print record was found, so we create a new one */
  107.     
  108.     thePrRecHdl = (THPrint)NewHandleClear( sizeof( TPrint ) );
  109.     if ( !thePrRecHdl )
  110.         return (THPrint)NULL;
  111.     
  112.     /* Set the default values into the print record */
  113.     
  114.     PrOpen();
  115.     
  116.     if ( PrError() == noErr ){
  117.             
  118.         PrintDefault( thePrRecHdl );
  119.         
  120.         if ( PrError() != noErr ){
  121.             DisposHandle( thePrRecHdl );
  122.             thePrRecHdl = (THPrint)NULL;
  123.         }
  124.     }else{
  125.         DisposHandle( thePrRecHdl );
  126.         thePrRecHdl = (THPrint)NULL;
  127.     }
  128.     PrClose();
  129.     
  130.     if ( thePrRecHdl == (THPrint)NULL )
  131.         return (THPrint)NULL;
  132.     
  133.     /* At this point, we have a good print record.  Save it into the
  134.      * preferences file
  135.      */
  136.  
  137.     UseResFile( gPrefFileRefNum );
  138.     
  139.     AddResource( thePrRecHdl, 'PStl', rPrintRecID, "\pPage Setup" );
  140.  
  141.     err = ResError();
  142.  
  143.     UseResFile( curFile );
  144.     
  145.     if ( err ){
  146.         DisposHandle( thePrRecHdl );
  147.         return (THPrint)NULL;
  148.     }
  149.     
  150.     WriteResource( thePrRecHdl );
  151.     UpdateResFile( gPrefFileRefNum );
  152.  
  153.     return thePrRecHdl;
  154. }